//
//  checkPower.m
//  QRMaster
//
//  Created by myApple on 15/10/28.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "checkPower.h"
#import "AppDelegate.h"
#import "OpertionPlist.h"

@implementation checkPower

-(void)getUserPower
{
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [rnk initWithPath:POST(ADDMASTERCARDWITHCOMMUNITY) params:postDict block:^(id downLoadData, id error) {
        if ([downLoadData objectForKey:@"data"] != nil) {
            //每请求一次就在本地数据中更新一次
            NSDictionary * communityDownLoadData = [downLoadData objectForKey:@"data"];
            NSArray * CommunityArray = [communityDownLoadData objectForKey:@"communities"];
            NSMutableDictionary * communityData = [OpertionPlist readDataFromPlist];
            if (communityData == nil) {
                communityData = [[NSMutableDictionary alloc]initWithObjectsAndKeys:CommunityArray,COMMUNITYINFOFORKEY,nil];//修改一个自己便于记忆的名称
                [OpertionPlist writeDataToPlist:communityData];
                
            }
            else
            {
                [communityData setValue:CommunityArray forKey:COMMUNITYINFOFORKEY];
                [OpertionPlist writeDataToPlist:communityData];
                
            }
            
        }
        else
        {
            if ([[error objectForKey:@"status"]integerValue] == 210)
            {
                NSMutableDictionary * communityData = [[NSMutableDictionary alloc]initWithObjectsAndKeys:nil,COMMUNITYINFOFORKEY,nil];//没有集群的时候
                [OpertionPlist writeDataToPlist:communityData];
                [[NSUserDefaults standardUserDefaults] setValue:nil forKey:@"communityId"];
            }
            
        }
        _powerBlock(downLoadData,error);
    }];
}


@end
